package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.comon.CustomException;
import com.briup.entity.Category;
import com.briup.entity.Dish;
import com.briup.entity.Setmeal;
import com.briup.mapper.CategoryMapper;
import com.briup.service.CategoryService;
import com.briup.service.DishService;
import com.briup.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: liao
 * @Date: 2022/9/12-09-12-12:02
 * @Description：com.briup.service.impl
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    
    @Override
    
    public void deleteById(Long id) {
        
        //1.查看当前分类是否关联了菜品，如果已经关联，则不能删除，报出一个业务异常
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据id进行查询
        queryWrapper.eq(Dish::getCategoryId,id);

        int count1 = dishService.count(queryWrapper);

        //1.如果已经关联菜品，就抛出一个业务异常
        if (count1>0){
            //就抛出一个业务异常
            throw new CustomException("该分类关联了菜品，不能删除");
        }

        //2.查看当前分类是否关联了套餐，如果已经关联，则不能删除，报出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //添加查询条件，根据id进行查询
     setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);

        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        
        if (count2>0){
            throw new CustomException("该分类关联了套餐，不能删除");
        }
        
        
        //如果都不是，那就正常删除
        super.removeById(id);
        
        


    }
}
